/*
#####################################################################
####################	   T R I B U I T 		#####################
#####################################################################

Copyright (C) 2010  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.


########################################################################
*/
package PictureDescription;

import java.util.*;

import javax.imageio.ImageIO;
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.io.File;

import GuiTools.*;
import Mainframe.Mainframe;
import PDPictureEditor.PictureEditorDialog;
import Settings.*;

/**
 * @author Christoph Schnack
 *
 */
public class PictureVocabularyManageDialog {
	Mainframe MAINFRAME;
	Settings SET;
	Language LANG;
	MDManager PMDM;
	PictureStorage PICSTO;
	MDTribuitFrame pvmdFrame;
	PictureCollection colToEdit;
	String cteName;
	PictureSelectionPanel psp;
	JButton editPUBtn,delPUBtn,viewPUBtn;
	
	public PictureVocabularyManageDialog(PictureStorage picsto,Settings set,Mainframe mf,MDManager pmdm,String cte){
		SET=set;
		LANG=SET.getLanguage();
		PICSTO=picsto;
		MAINFRAME=mf;
		PMDM=pmdm;
		colToEdit=PICSTO.getCollection(cte);
		cteName=cte;
		buildPVMD();
	}
	
	public void buildPVMD(){
		pvmdFrame = new MDTribuitFrame(LANG.getEditPictureCollection(),PMDM);
		HashMap<String,Image> data = new HashMap<String,Image>();
		if(colToEdit.hasPictures()){
			Vector<PictureUnit> content = colToEdit.getPictureUnits();
			PictureUnit pu;
			Image img;
			for(int i=0;i<content.size();i++){
				pu=content.elementAt(i);
				//TODO PATH USED
				img = GuiTools.loadImage(GuiTools.PATHTOPICTUREMAPS+File.separator+cteName+File.separator+pu.getName()+File.separator+"image");
				if(img!=null)data.put(pu.getName(),img);
			}
		}
		psp=new PictureSelectionPanel(SET,this,pvmdFrame,data);
		//Buttons
		EditButton addPUBtn = new EditButton("+",true);
		addPUBtn.addActionListener(new AddPUBtnListener());
		addPUBtn.setToolTipText(LANG.getCreatePictureUnit());
		delPUBtn = new EditButton("-",colToEdit.hasPictures());
		delPUBtn.addActionListener(new DelPUBtnListener());
		delPUBtn.setToolTipText(LANG.getDeletePictureUnit());
		editPUBtn = new EditButton("edt",colToEdit.hasPictures());
		editPUBtn.addActionListener(new EditPUBtnListener());
		editPUBtn.setToolTipText(LANG.getEditPictureUnit());
		//TODO PATH USED
		viewPUBtn = new EditButton(GuiTools.loadImageFromGraphics("viewButtonIcon.png"),GuiTools.loadImageFromGraphics("viewButtonIconHL.png"),GuiTools.loadImageFromGraphics("viewButtonIconGray.png"),colToEdit.hasPictures());
		viewPUBtn.addActionListener(new ViewPUBtnListener());
		viewPUBtn.setToolTipText(LANG.getViewPictureUnit());
		JPanel editButtonPanel = new JPanel();
		editButtonPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		editButtonPanel.add(addPUBtn);
		editButtonPanel.add(delPUBtn);
		editButtonPanel.add(editPUBtn);
		editButtonPanel.add(viewPUBtn);
		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		centerPanel.add(psp,gbc);
		gbc.gridy=1;
		centerPanel.add(editButtonPanel,gbc);
		
		JButton closeButton = new JButton(LANG.getClose());
		closeButton.addActionListener(new CloseBtnListener());
		JPanel buttonPanel = new JPanel();
		buttonPanel.setBackground(GuiTools.BGCOLOR);
		buttonPanel.add(closeButton);
		
		pvmdFrame.getContentPane().add(BorderLayout.NORTH,new Header(LANG.getEditPictureCollection()));
		pvmdFrame.getContentPane().add(BorderLayout.CENTER,centerPanel);
		pvmdFrame.getContentPane().add(BorderLayout.SOUTH,buttonPanel);
		pvmdFrame.showUpAt(MAINFRAME);
		pvmdFrame.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				new PictureManageDialog(PICSTO,SET,MAINFRAME,PMDM);
			}
		});
	}

	
	class AddPUBtnListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			new PictureEditorDialog(PICSTO,SET,MAINFRAME,PMDM,cteName);
			pvmdFrame.dispose();
		}
	}
	
	class DelPUBtnListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(psp.getSelection()!=null){
				int ans = JOptionPane.showConfirmDialog(pvmdFrame, LANG.getReallyDeleteThisPictureMap(), LANG.getAttention(), JOptionPane.YES_NO_OPTION);
				if(ans == JOptionPane.YES_OPTION){
					PICSTO.deletePictureUnitInCollection(psp.getSelection(), colToEdit.getName());
					PICSTO.deleteUnitFromLessons(colToEdit.getName(),psp.getSelection());
					psp.removeFromList(psp.getSelection());
					if(!colToEdit.hasPictures()){
						delPUBtn.setEnabled(false);
						editPUBtn.setEnabled(false);
						viewPUBtn.setEnabled(false);
					}
					pvmdFrame.repaint();
				}
			}
			else JOptionPane.showMessageDialog(pvmdFrame,LANG.getNothingChosen());
		}
	}
	
	class EditPUBtnListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(psp.getSelection()!=null){
				openEditDialog(psp.getSelection());
			}
			else JOptionPane.showMessageDialog(pvmdFrame,LANG.getNothingChosen());
		}
	}
	
	class ViewPUBtnListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(psp.getSelection()!=null){
				new PictureViewer(SET,MAINFRAME,cteName,colToEdit.getPictureUnit(psp.getSelection()));
			}
			else JOptionPane.showMessageDialog(pvmdFrame,LANG.getNothingChosen());
		}
	}
	
	public void openEditDialog(String selection){
		String collection = colToEdit.getName(),name = selection;
		File permissionTest = new File(GuiTools.PATHTOPICTUREMAPS+File.separator+collection+File.separator+name+File.separator+"data");
		if(permissionTest.exists() && !permissionTest.canWrite()){
			JLabel msgLabel = new JLabel(LANG.getEditingNotAllowedFor()+": "+name);
			msgLabel.setFont(new Font(Font.SANS_SERIF,Font.BOLD,14));
			JLabel pathLabel1 = new JLabel(LANG.getNoWritePermissionForFile()+":");
			JLabel pathLabel2 = new JLabel(permissionTest.getAbsolutePath());
			JPanel msgPanel = new JPanel();
			msgPanel.setLayout(new BoxLayout(msgPanel,BoxLayout.Y_AXIS));
			msgPanel.add(msgLabel);
			msgPanel.add(pathLabel1);
			msgPanel.add(pathLabel2);
			JOptionPane.showMessageDialog(pvmdFrame, msgPanel, LANG.getError(), JOptionPane.ERROR_MESSAGE);
			return;
		}
		new PictureEditorDialog(PICSTO,SET,MAINFRAME,PMDM,cteName,colToEdit.getPictureUnit(selection));
		pvmdFrame.dispose();
	}
	
	
	class CloseBtnListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			pvmdFrame.dispose();
			new PictureManageDialog(PICSTO,SET,MAINFRAME,PMDM);
		}
	}
}
